<template>
	<view class="sortBox">
		<!-- 搜索框 -->
		<view class="searchBox">
			<u-skeleton rows="1" :title="false" rowsHeight="43" :loading="skeletonLoading">
				<u-search :showAction="false" placeholder="请输入搜索关键字..." v-model="searchWord"></u-search>
			</u-skeleton>
		</view>

		<!-- 分享按钮 -->
		<u-skeleton style="width: 700rpx; margin-top: 20rpx" rows="1" :title="false" rowsHeight="43" :loading="skeletonLoading">
			<view class="inviteBox">
				<view class="decoration" />
				<view class="title">饭后点心, 从分享开始</view>
				<view class="inviteBut">
					<u-icon name="share-square" color="#fff" size="20"></u-icon>
					邀请好友拼单
				</view>
			</view>
		</u-skeleton>

		<!-- 垂直选项卡列表 -->
		<view class="sortNavBox">
			<!-- 左侧菜单 -->
			<u-sticky>
				<scroll-view class="menu" scroll-y>
					<u-skeleton rows="1" rowsHeight="965rpx" :title="false" :loading="skeletonLoading">
						<view v-for="(item, index) in sortNavList" :key="item._id" :class="['menu-item', { active: currentNavIndex === index }]" @tap="sortNavBoxChangeTab(index)">
							<image :src="item.picurl" mode="aspectFill"></image>
							<view>{{ item.name }}</view>
						</view>
					</u-skeleton>
				</scroll-view>
			</u-sticky>

			<!-- 右侧列表 -->
			<view class="content">
				<u-sticky offset-top="-1">
					<u-skeleton rows="1" rowsHeight="43" :title="false" :loading="skeletonLoading">
						<view class="filterBox">
							<view class="item" @click="filterIndexChange(index)" :class="{ active: currentFilterIndex === index }" v-for="(item, index) in filterList" :key="index">
								{{ item }}
								<view class="icon" v-if="item == '价格'">
									<u-icon name="arrow-up-fill" color="#c4c6c9" size="6"></u-icon>
									<u-icon name="arrow-down-fill" color="#c4c6c9" size="6"></u-icon>
								</view>
							</view>
						</view>
					</u-skeleton>
				</u-sticky>
				<!-- 商品列表 -->
				<scroll-view style="height: 100vh" scroll-y="true">
					<view class="productListBox">
						<view class="productList" v-for="(item, index) in productInfoList" :key="item._id">
							<u-skeleton rows="1" style="height: 500rpx" rowsHeight="500rpx" :title="false" :loading="skeletonLoading">
								<view class="producItemBox">
									<view class="imgBox">
										<image :src="item.picurls" mode="aspectFill"></image>
									</view>
									<view class="textBox">
										<view>
											<view class="proName">
												{{ item.name }}
											</view>
											<view class="proSales">销量：{{ item.sales }}</view>
										</view>
										<view class="proPrice_unit">
											<view class="price">￥{{ item.price }}/{{ item.unit }}</view>
											<view class="selectNumber">
												<u-number-box
													v-model="item.selectNumber"
													integer
													step="1"
													min="0"
													max="999"
													@change="changeSelectProductNumber"
													:name="JSON.stringify(item)"
												></u-number-box>
											</view>
										</view>
									</view>
								</view>
							</u-skeleton>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<!-- 购车车 -->
		<view class="shoppingCartBox" v-if="userSelectProductInfoList.length">
			<view class="cart" @click="toOrderDetails">
				<u-icon name="shopping-cart" color="82848a" size="35"></u-icon>
				购物车
				<view class="number">{{ calcTotalPrice.productCount }}</view>
			</view>
			<view class="totalPrice">￥{{ calcTotalPrice.totalPrice }}元</view>
			<view class="settlement">结算</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 搜索框文本
			searchWord: '',
			// 左侧导航菜单列表
			sortNavList: [],
			// 左侧导航菜单当前选项
			currentNavIndex: 0,
			// 商品列表过滤器
			filterList: ['综合', '销量', '新品', '价格'],
			// 商品列表过滤器当前选项
			currentFilterIndex: 0,
			// 骨架屏加载状态
			skeletonLoading: true,
			// 商品信息列表
			productInfoList: [],
			// 用户选择商品信息列表
			userSelectProductInfoList: []
		};
	},
	onReady() {
		this.getSortNav();
		// // setTimeout(() => {
		// this.sortNavList = uni.getStorageSync('sortNavList');
		// this.productInfoList = uni.getStorageSync('productInfoList');
		// this.initShoppingCart();
		// this.skeletonLoading = false;
		// // }, 300);
	},
	onShow() {
		this.initShoppingCart();
	},
	onPullDownRefresh() {
		this.sortNavBoxChangeTab(this.currentNavIndex);
	},
	computed: {
		calcTotalPrice() {
			let totalPrice = 0;
			let productCount = 0;
			this.userSelectProductInfoList.forEach((product) => {
				totalPrice += product.price * product.selectNumber;
				productCount += product.selectNumber;
			});
			return {
				totalPrice,
				productCount
			};
		}
	},
	methods: {
		// 到购物车详情页
		toOrderDetails() {
			uni.navigateTo({
				url: '/pages/orderDetails/orderDetails'
			});
		},
		initShoppingCart() {
			const arr = uni.getStorageSync('userSelectProductInfoList') || [];
			this.userSelectProductInfoList = arr;
			this.productInfoList.forEach((item) => {
				let temp = this.userSelectProductInfoList.find((val) => val._id === item._id);
				if (temp) {
					item.selectNumber = temp.selectNumber;
				} else {
					item.selectNumber = 0;
				}
			});
		},
		changeSelectProductNumber(e) {
			let { value, name } = e;
			name = JSON.parse(name);

			// 使用Set快速查找产品ID是否已存在
			let productIds = new Set(this.userSelectProductInfoList.map((product) => product._id));

			if (productIds.has(name._id)) {
				// 如果产品ID已存在，直接更新对应的selectNumber值
				this.userSelectProductInfoList.forEach((product) => {
					if (product._id === name._id) {
						product.selectNumber = value;
					}
				});
			} else {
				// 如果产品ID不存在，创建一个新的产品对象并添加到列表中
				name.selectNumber = value;
				this.userSelectProductInfoList.push(name);
			}

			// 过滤掉selectNumber为0的产品
			this.userSelectProductInfoList = this.userSelectProductInfoList.filter((product) => product.selectNumber > 0);
			if (this.userSelectProductInfoList.length) {
				uni.setStorageSync('userSelectProductInfoList', this.userSelectProductInfoList);
			} else {
				uni.removeStorageSync('userSelectProductInfoList');
			}
		},
		getProductInfo() {
			uni.showLoading({
				title: '数据加载中...',
				mask: true
			});
			let selectName = this.sortNavList[this.currentNavIndex].name;
			uniCloud
				.callFunction({
					name: 'get_productInfo_rows',
					data: {
						name: selectName
					}
				})
				.then((res) => {
					// 给后端获取到的每一项数据添加一个属性selectNumber，统计用户选择购买数默认为0
					this.productInfoList = res.result.data;
					Object.keys(this.productInfoList).forEach((val) => {
						this.productInfoList[val].selectNumber = 0;
					});
					uni.setStorageSync('productInfoList', this.productInfoList);
					this.initShoppingCart();
					this.skeletonLoading = false;
					uni.stopPullDownRefresh();
					uni.hideLoading();
				});
		},
		filterIndexChange(index) {
			this.currentFilterIndex = index;
		},
		sortNavBoxChangeTab(index) {
			this.currentNavIndex = index;
			this.getProductInfo();
		},
		getSortNav() {
			uniCloud
				.callFunction({
					name: 'get_sortNav_rows'
				})
				.then((res) => {
					uni.setStorageSync('sortNavList', res.result.data);
					this.sortNavList = res.result.data;
					this.getProductInfo();
				});
		},
		leftClickToIndex() {
			uni.reLaunch({
				url: '/pages/index/index'
			});
		}
	}
};
</script>

<style lang="scss">
.sortBox {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;

	.searchBox {
		width: 700rpx;
	}

	// 邀请好友拼单盒子样式
	.inviteBox {
		width: 700rpx;
		background-color: #f2f2f2;
		display: flex;
		align-items: center;
		padding-left: 12rpx;
		font-size: 20rpx;
		border-radius: 0 30rpx 30rpx 0;
		color: #6a6a6a;
		margin-top: 20rpx;

		.decoration {
			width: 8rpx;
			height: 28rpx;
			margin-right: 15rpx;
			border-radius: 10rpx;
			background-color: #f3a73f;
		}

		.inviteBut {
			font-size: 28rpx;
			color: white;
			background-color: #f3a73f;
			padding: 10rpx 18rpx;
			border-radius: 30rpx;
			display: flex;
			margin-left: auto;
		}
	}

	.sortNavBox {
		width: 750rpx;
		display: flex;
		align-items: flex-start;
		margin-top: 20rpx;

		.menu {
			width: 170rpx;
			height: 967rpx;

			.menu-item {
				width: 150rpx;
				height: 150rpx;
				font-size: 22rpx;
				color: #333;
				margin: 10rpx;
				border: 1rpx solid #dfe0e2;
				border-radius: 15rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				box-sizing: border-box;

				image {
					width: 90rpx;
					height: 90rpx;
				}

				&.active {
					color: #f3a73f;
					border: 1rpx solid #f3a73f;
				}
			}
		}

		.content {
			flex: 1;
			box-sizing: border-box;
			margin-bottom: 160rpx;

			.filterBox {
				width: 100%;
				height: 80rpx;
				display: flex;
				box-sizing: border-box;
				background-color: white;
				padding: 0 10rpx;

				.item {
					flex: 1;
					// border: 1px solid #6a6a6a;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 23rpx;

					.icon {
						margin-left: 5rpx;
					}

					&.active {
						font-weight: bold;
					}
				}
			}

			.productListBox {
				padding: 10rpx;
				box-sizing: border-box;
				display: grid;
				grid-template-columns: repeat(2, 1fr);
				gap: 10rpx;

				.productList {
					flex: 1;
					height: 500rpx;
					border: 1rpx solid #dfe0e2;

					.producItemBox {
						width: 100%;
						height: 100%;

						.imgBox {
							width: 100%;
							height: 280rpx;
							box-sizing: border-box;
							overflow: hidden;

							image {
								width: 100%;
								height: 100%;
							}
						}

						.textBox {
							width: 100%;
							height: 220rpx;
							box-sizing: border-box;
							padding: 10rpx;
							display: flex;
							flex-direction: column;
							justify-content: space-between;

							.proName {
								display: -webkit-box;
								-webkit-box-orient: vertical;
								-webkit-line-clamp: 2;
								overflow: hidden;
							}

							.proSales {
								font-size: 25rpx;
								color: #c8c9cc;
								margin-top: 12rpx;
							}

							.proPrice_unit {
								.price {
									font-size: 32rpx;
									color: #fa3534;
								}

								.selectNumber {
									display: flex;
									justify-content: center;
								}
							}
						}
					}
				}
			}
		}
	}

	.shoppingCartBox {
		background-color: #e8e8e8;
		width: 750rpx;
		height: 100rpx;
		position: fixed;
		bottom: var(--window-bottom, 0);
		z-index: 999;
		display: flex;
		justify-content: flex-end;
		align-items: flex-end;
		.cart {
			background-color: white;
			width: 200rpx;
			height: 100rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			left: 30rpx;
			bottom: 20rpx;
			border-radius: 50rpx;
			box-shadow: 0 0 2rpx #c8c9cc;
			box-sizing: border-box;
			.number {
				background-color: #fa3534;
				width: 38rpx;
				height: 38rpx;
				border-radius: 50%;
				font-size: 23rpx;
				font-weight: bold;
				color: white;
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 0;
				right: 0;
			}
		}
		.totalPrice {
			// border: 1rpx solid white;
			width: 200rpx;
			height: 100%;
			color: #fa3534;
			font-size: 36rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.settlement {
			background-color: #fa3534;
			width: 300rpx;
			height: 100rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-top-left-radius: 100rpx;
			border-bottom-left-radius: 100rpx;
			color: white;
			font-size: 40rpx;
		}
	}
}
</style>
